/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.toolkit.dao;

import java.awt.geom.Point2D;

import org.jdom.Element;

import who.tmap.dao.TMapDAOException;
import who.tmap.toolkit.shape.MapShape;
import who.tmap.toolkit.shape.MapShapes;
import who.tmap.toolkit.shape.annotation.GraphicAnnotationShape;

public class GraphicAnnotationShapeDAO extends BaseMapShapeDAO {

	public MapShape load( Element elt ) throws TMapDAOException {
		String name =elt.getName();
		
		int shapeType =MapShapes.UNDEFINED;
		
        for( int ct =0; ct < MapShapes.names.length; ct++ ) {
            if ( MapShapes.names[ ct ].equals( name ) ) {
            	shapeType =ct;
            	break;
            }
        }
		
        return load( elt, shapeType );
	}
	
	protected MapShape load( Element elt, int shapeType ) throws TMapDAOException {
	    GraphicAnnotationShape shape =null;
	    
        try {
        	shape = (GraphicAnnotationShape)msf.getGraphic( shapeType );
            
            Element colorElt =elt.getChild( "Color" );
            shape.setColor( getColorFromHexa( colorElt.getAttribute( "rgb" ).getValue() ) );
            Element fillColorElt =elt.getChild( "FillColor" );
            shape.setFillColor( getColorFromHexa( fillColorElt.getAttribute( "rgb" ).getValue() ) );            
            
            Element ptElt =elt.getChild( "Point" );
            Point2D pt =new Point2D.Double(
                    			ptElt.getAttribute( "x" ).getDoubleValue(),
                    			-ptElt.getAttribute( "y" ).getDoubleValue() );
            shape.setPt( pt );
            
            Element dimElt =elt.getChild( "Dimension" );
            shape.setWidth( dimElt.getAttribute( "width" ).getDoubleValue() );
            shape.setHeight( dimElt.getAttribute( "height" ).getDoubleValue() );
                        
        } catch (Exception e) {
            throw new TMapDAOException( e );
        }
        
        return shape;
	}
	
	public Element save( MapShape mapShape ) throws TMapDAOException {
		GraphicAnnotationShape shape =(GraphicAnnotationShape)mapShape;
	    Element shapeElt =new Element( MapShapes.names[ shape.getType() ] );
	    
        Element colorElt =new Element( "Color" );
        shapeElt.addContent( colorElt );
        colorElt.setAttribute( "rgb", Integer.toHexString( shape.getColor().getRGB() ) );
        
        Element fillColorElt =new Element( "FillColor" );
        shapeElt.addContent( fillColorElt );
        fillColorElt.setAttribute( "rgb", shape.getFillColor() != null 
        											? Integer.toHexString( shape.getFillColor().getRGB() )
        											: "" );    
        
        Element ptElt =new Element( "Point" );
        shapeElt.addContent( ptElt );
        
        ptElt.setAttribute( "x", Double.toString( shape.getPt().getX() ) );
        ptElt.setAttribute( "y", Double.toString( -shape.getPt().getY() ) );
        
        Element dimElt =new Element( "Dimension" );
        shapeElt.addContent( dimElt );
        
        dimElt.setAttribute( "width", Double.toString( shape.getWidth() ) );
        dimElt.setAttribute( "height", Double.toString( shape.getHeight() ) );

        return shapeElt;
	}
}
